import time
import datetime

from engine.Calendar import *

def getHourInDay(date):
    return date.hour + date.minute/60.0 + date.second/3600

def GetCalendarCode(person, date):
    code = "\n"
    code = code + "<div class='calendarNavigation'>\n"
    code = code + "  <div class='calendarNavPrev'>\n"
    code = code + "   <a href=/?date='%s'>&lt;&lt;</a>\n" % (time.strftime("%d/%m-%Y", time.localtime()))
    code = code + "  </div>\n"
    code = code + "  <div class='calendarNavDate'>\n"
    code = code + "   %s " % (time.strftime("%d/%m-%Y"))
    code = code + "  </div>\n"
    code = code + "  <div class='calendarNavNext'>\n"
    code = code + "    <a href=/?date='%s'>&gt;&gt;</a>\n" % (time.strftime("%d/%m-%Y", time.localtime()))
    code = code + "  </div>\n"
    code = code + "</div>\n"
    code = code + "<div class='calendarBox'>\n"
    code = code + "<div class='calendarTimes'>\n"
    code = code + "<ul class='calendarHours'>"
    for i in range(25):
        #code = code + "  <div class='calendarHour'>"
        code = code + "<li>%i:00-</li>" % (i)
        #code = code + "</div><br>\n"
    code = code + "</ul>\n"
    code = code + "</div>\n"
    code = code + "<div class='calendarContent'>\n"
    code = code + "  <div class='calendarItem' style='height: 10px; background-color: #FFFFFF;'></div>"
    calEntries = person.getCalendar().getEntriesAtDate(date)
    offset = 0
    colorCode = 0
    for calEntry in calEntries:
        if colorCode % 2 == 0:
            color = "#FFAAAA"
        else:
            color = "#AAFFAA"
        colorCode = colorCode + 1
        hour = getHourInDay(calEntry.date)
        if hour > offset:
            dy = int(20.0*(hour-offset))
            offset = hour
            code = code + "  <div class='calendarItem' style='height: %ipx; background-color: #FFFFFF;'></div>" % (dy)
        duration = min(calEntry.duration, 24-offset)
        height = int(20.0*duration+1)
        if calEntry.activity.type == "travel":
            color = "#8888FF"
        else:
            colorCode = colorCode + 1

        if height < 14:
            code = code + "  <div class='calendarItem' style='height: %ipx; background-color: %s;'>%s</div>" \
                        % (height, color, "")
        else:
            code = code + "  <div class='calendarItem' style='height: %ipx; background-color: %s;' mouseOver='hallo'>%s</div>" \
                        % (height, color, calEntry.activity.name)
        offset = offset + calEntry.duration
    code = code + "</div>\n"
    code = code + "</div>\n"
    return code
